<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>派发事件parent</title>
    <style>
        template{
            display: none;
        }
    </style>
</head>
<!-- Vue2.0中events选项被弃用，$dispatch和$broadcast被弃用 -->
<body>
    <div id="app">
        <p>{{ message }}</p>
        <child-component ref="child"></child-component>
    </div>
    <template id='childComponent'>
        <div>
            <input type="text" v-model="msg"/>
            <button @click="notify">dispatch</button>
        </div>
    </template>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    // 将在各处使用该事件中心
    // 组件通过它来通信
    var eventHub = new Vue();

    Vue.component('child-component', {
        template: '#childComponent',
        data: function(){
            return {
                msg: ''
            }
        },
        methods: {
            notify: function(){
                if(this.msg.trim()){
                    this.$parent.addToMessage(this.msg);
                    this.msg = '';
                }
            }
        }
    });
    
    var app = new Vue({
        el: "#app",
        data: {
            message: ['dsa','dsa1']
        },
        methods: {
            'addToMessage': function(msg){
                this.message.push(msg);
            }
        }
    });
</script>
</html>